class Sales::FirmsController < ApplicationController
  before_action :set_sales_firm, only: [:show, :grab, :edit, :update, :destroy]

  # GET /sales/firms
  # GET /sales/firms.json
  def index
    @html_title ||= '公司库'
    params[:q] ||= {}
    @search = Sales::Firm.this.search params[:q]
    @sales_firms = @search.result.distinct.page(params[:page] || 1).per(2).includes(:province, :city, :leads => [:user])
  end

  def my_leads
    @html_title ||= '我的leads'
    params[:q] ||= {}
    params[:q][:leads_user_id_eq] = current_user.id
    index
    render :action=> :index
  end

  # GET /sales/firms/1
  # GET /sales/firms/1.json
  def show
  end

  # GET /sales/firms/new
  def new
    @html_title = '添加公司'
    @sales_firm = Sales::Firm.new
    @need_vue = true
  end

  # GET /sales/firms/1/edit
  def edit
    @html_title = '公司信息编辑'
    render layout: 'modal'
  end

  def grab
    if @sales_firm.main_lead.user
      return error_notice "公司leads现被#{@sales_firm.main_lead.user}在跟进"
    end
    @sales_firm.main_lead.grab_by(current_user.id)
    show_js_notice('获取成功！')
  end

  def detail_edit
    @html_title = '公司工商信息编辑'
    if params[:id].present?
      @firm_detail = Sales::FirmDetail.find(params[:id])
      @sales_firm = @firm_detail.firm
    else
      @sales_firm = Sales::Firm.this.find(params[:firm_id])
      @firm_detail = @sales_firm.firm_detail || @sales_firm.build_firm_detail
      @firm_detail.register_money_type ||= 1
    end
    render layout: 'modal'
  end

  def detail_save
    if params[:sales_firm_detail][:register_time].present?
      params[:sales_firm_detail][:register_time] = "#{params[:sales_firm_detail][:register_time]}-1"
    end
    if params[:sales_firm_detail][:id].present?
      @sales_firm_detail = Sales::FirmDetail.find params[:sales_firm_detail][:id]
      @sales_firm_detail.update(sales_firm_detail_params)
    else
      @sales_firm_detail = Sales::FirmDetail.create(sales_firm_detail_params)
    end
    if @sales_firm_detail.valid?
      show_js_notice('保存成功！')
    else
      error_notice(@sales_firm)
    end
  end

  # POST /sales/firms
  # POST /sales/firms.json
  # 这里的create是vue提交过来的，返回json数据
  def create
    @sales_firm = Sales::Firm.this.new(sales_firm_params)
    respond_to do |format|
      if @sales_firm.save
        @sales_firm.main_lead.grab_by(current_user.id)
        format.html { redirect_to @sales_firm , notice: '创建成功'}
        format.json { render json: @sales_firm.to_json }
      else
        format.html { render :new }
        format.json { render json: @sales_firm.errors.values.join('，'), status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /sales/firms/1
  # PATCH/PUT /sales/firms/1.json
  def update
    if @sales_firm.update(sales_firm_params)
      show_js_notice '修改成功'
      # format.html { redirect_to @sales_firm, notice: '修改成功' }
      # format.json { render :show, status: :ok, location: @sales_firm }
    else
      error_notice(@sales_firm)
      # format.html { render :edit }
      # format.json { render json: @sales_firm.errors, status: :unprocessable_entity }
    end
  end

  # DELETE /sales/firms/1
  # DELETE /sales/firms/1.json
  def destroy
    @sales_firm.destroy
    show_js_notice '修改成功'
    # respond_to do |format|
    #   format.html { redirect_to sales_firms_url, notice: 'Firm was successfully destroyed.' }
    #   format.json { head :no_content }
    # end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_sales_firm
      @sales_firm = Sales::Firm.this.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def sales_firm_params
      params.require(:sales_firm).permit!
    end

    def sales_firm_detail_params
      params.require(:sales_firm_detail).permit!
    end
end
